<script type="text/javascript">
    // 单图上传
    function one_upload(upload,$){
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: "{:url('upload/upload')}" //改成您自己的上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code === 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                let image = res.data[0].file_url

                $("#test2").attr("value",image);

            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    }
    // 单图上传
    function one_upload_copy(upload,$){
        var uploadInst = upload.render({
            elem: '#test3'
            ,url: "{:url('upload/upload')}" //改成您自己的上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo2').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code === 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                let image = res.data[0].file_url

                $("#test4").attr("value",image);

            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText1');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    }


    // 视频上传
    function upload_video(upload,$){
        var uploadInst=upload.render({

            elem: '#video'
            ,url: "{:url('upload/upload')}"
            ,field:"file"
            ,data:{"file":"media"}
            ,accept: 'video' //视频
            // ,before:function (obj) {
            //     $('#demo9').css('display','block').attr('src', "http://p6nngxvb7.bkt.clouddn.com/FsyjSltTtkVtzepa_w7zsnS_S7zO"); //链接（base64）http://p6nngxvb7.bkt.clouddn.com/FsyjSltTtkVtzepa_w7zsnS_S7zO
            // }
            ,done: function(res){

                if(res.code == 0){
                    return layer.msg(res.message);
                }
                if(res.code==1){

                    var file_url = res.data[0].file_url;

                    $("#videourl").val(file_url);

                    $("#demo9").attr("src",file_url);

                    layer.alert("上传成功",{offset:['200px','450px'],icon:6});

                }
            }
            ,error:function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    }

    // 视频预览
    function video_view() {
        var classVideo = document.getElementById("videourl").value;
        var index = layer.open({
            title:'浏览视频',
            type: 2,
            content: classVideo,
            area: ['600px', '450px'],
            offset:'t',
            maxmin: true,
            end: function () {

            }
        });
    }

    // 多图上传
    function many_upload(upload,$){
        var files = [];

        //多文件列表示例
        var demoListView = $('#demoList')
            ,uploadListIns = upload.render({
            elem: '#testList'
            //,url: 'https://httpbin.org/post' //改成您自己的上传接口
            ,url: "{:url('upload/upload')}" //改成您自己的上传接口
            ,accept: 'file'
            ,multiple: true
            ,auto: false
            ,bindAction: '#testListAction'
            ,choose: function(obj){
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        // ,'<td>'+ file.name +'</td>'
                        ,'<td><img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img"> </td>'
                        ,'<td>'+ (file.size/1024).toFixed(1) +'kb</td>'
                        ,'<td>等待上传</td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            ,done: function(res, index, upload){

                if (res.code == 1){

                    let one_file = res.data[0].file_url;
                    files.push(one_file)
                    $(".many_upload").attr("value",files);

                    var tr = demoListView.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作

                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            ,error: function(index, upload){
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
    }

</script>